local L = LibStub("AceLocale-3.0"):GetLocale("AzCastBarOptions")
local GetTime = GetTime;
local FormatTime = AzCastBar.FormatTime;
local b = AzCastBar.CreateBar();

-- Extra Options
local extraOptions = {
	{
		[0] = L["Additional"],
		{ type = "Color", var = "colNormal", default = { 0.4, 0.6, 0.8, 1 }, label = L["Global Cooldown Bar Color"] },
	},
};

--------------------------------------------------------------------------------------------------------
--                                            Frame Scripts                                           --
--------------------------------------------------------------------------------------------------------

-- OnUpdate
local function OnUpdate(self,elapsed)
	-- Update
	if (not self.fadeTime) then
		self.timeLeft = max(0,self.startTime + self.duration - GetTime());
		self.status:SetValue(GetTime() - self.startTime);
		self.time:SetText(FormatTime(self.timeLeft)..(self.cfg.showTotalTime and " / "..FormatTime(self.duration,1) or ""));
		if (self.timeLeft == 0) then
			self.fadeTime = self.cfg.fadeTime;
		end
	-- Fadeout
	elseif (self.fadeElapsed <= self.fadeTime) then
		self.fadeElapsed = (self.fadeElapsed + elapsed);
		self:SetAlpha(self.cfg.alpha - self.fadeElapsed / self.fadeTime * self.cfg.alpha);
	else
		self:Hide();
	end
end

-- OnEvents
local function OnEvent(self,event,unit,spell,rank,...)
	-- End if Wrong Unit
	if (unit ~= "player") then
		return;
	-- Start GCD -- START events are for casts, SUCCEEDED are for instants
	elseif (event == "UNIT_SPELLCAST_START" or event == "UNIT_SPELLCAST_SUCCEEDED") then
		local startTime, duration, enabled = GetSpellCooldown(spell,rank);
		if (duration) and (duration > 0) then
			self.startTime, self.duration, self.enabled = startTime, duration, enabled;
			self.status:SetMinMaxValues(0,self.duration);
			self.fadeTime = nil;
			self.fadeElapsed = 0;
			self:SetAlpha(b.cfg.alpha);
			self:Show();
		end
	-- Abort GCD
	elseif (event == "UNIT_SPELLCAST_STOP") then
		self.fadeTime = self.cfg.fadeTime;
	end
end

-- Config Changed
function b:OnConfigChanged(cfg,barId)
	if (cfg.enabled) then
		self:RegisterEvent("UNIT_SPELLCAST_START");
		self:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED");
		self:RegisterEvent("UNIT_SPELLCAST_STOP");
		self.status:SetStatusBarColor(unpack(cfg.colNormal));
	else
		self:UnregisterAllEvents();
	end
end

--------------------------------------------------------------------------------------------------------
--                                        Init AzCastBar Entry                                        --
--------------------------------------------------------------------------------------------------------

b.token = "GlobalCooldown";
b.options = extraOptions;
b.name:SetText(GetSpellInfo(61304));
b.icon:SetTexture(select(3,GetSpellInfo(61304)));
b:ClearAllPoints();
b:SetPoint("CENTER");
b:SetScript("OnEvent",OnEvent);
b:SetScript("OnUpdate",OnUpdate);
tinsert(AzCastBar.frames,b);